//
//  VoicePlayer.m
//  RadioUrl
//
//  Created by ios on 16/4/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "VoicePlayer.h"
#import <AFNetworking.h>
#import "NetWorking.h"
@implementation VoicePlayer
static VoicePlayer *voicePlayer;


+(VoicePlayer *)shareVoicePlayer{
    static dispatch_once_t onceTaken;
    dispatch_once(&onceTaken, ^{
        
        voicePlayer = [[VoicePlayer alloc]init];
    });
    return  voicePlayer;
}
//-(RadioModel *)currentRadioModel{
//    if (_currentRadioModel == nil) {
//        _currentRadioModel =[[RadioModel alloc]init];
//        
//    }
//    return _currentRadioModel;
//}

-(AVPlayer *)player{
    if (_player == nil) {
        _player =[[AVPlayer alloc]init];
    }
    return _player;
}

-(void)play{
    if (self.playerItem) {
        [self.playerItem removeObserver:self forKeyPath:@"status" context:nil];
    }
    self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.currentRadioModel.radioPath]];
    [self.playerItem addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew) context:nil];
    
    [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    switch ([change[@"new"] integerValue]) {
        case AVPlayerItemStatusFailed:
            NSLog(@"缓冲失败");
            break;
        case AVPlayerItemStatusReadyToPlay:
            NSLog(@"缓冲成功");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"readToPlay" object:nil];
            self.readToPlay = YES;
            [self  startPlayvoice];
            break;
        case AVPlayerItemStatusUnknown:
            NSLog(@"缓冲未知");
            break;
        default:
            break;
    }
}


//-(void)currentRadioPlay{
//    __weak VoicePlayer *weakSelf = self;
//   
////    NSString *radioSource = [NSString stringWithFormat:@"http://www.ximalaya.com/tracks/%@.json",self.currentRadioModel.radioSource];
//    [NetWorking searchWithJsonUrl:self.currentRadioModel.radioSource success:^(RadioModel *radioModel) {
//        weakSelf.currentRadioModel = radioModel;
//        [weakSelf play];
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error);
//    }];
//}
-(void)startPlayvoice{
    if (self.readToPlay) {
        [self.player play];
        self.isPlaying = YES;
        if (self.timer.valid) {
            [self.timer invalidate];
        }
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(runTimerAction) userInfo:nil repeats:YES];
    }
}
-(void)runTimerAction{
    if (self.isPlaying) {
        self.songProgress = self.player.currentTime.value/self.player.currentTime.timescale;
        [self.delegate sendSongProgressMessage:self.songProgress];
    }
}
//暂停或播放
-(void)playeBtnPressing{
    if (self.isPlaying) {
        [self.player pause];
        
    }else{
        [self.player play];
        
    }
    self.isPlaying =!self.isPlaying;
}
//到指定时间播放
- (void)seekPlayerToTime:(CGFloat)time{
    if (self.isPlaying) {
        [self.player pause];
        [self.player seekToTime:CMTimeMakeWithSeconds(time, self.player.currentTime.timescale) completionHandler:^(BOOL finished) {
            [self startPlayvoice];
        }];
    }else{
        [self.player seekToTime:CMTimeMakeWithSeconds(time, self.player.currentTime.timescale) completionHandler:nil];
    }

}
@end
